package com.aaa.controller;

import com.aaa.entity.F_Menu;
import com.aaa.entity.F_TreeVo;
import com.aaa.service.F_QuanXianService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/quanxian")
public class F_QuanXianController {
    @Autowired
    F_QuanXianService service;

    /**
     * 查询所有部门
     * @param map
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public Object List(@RequestBody Map map){
        System.out.println(map);
        List<Map> data = service.selectList(map);
        Integer total = service.getCount(map);

        Map zmap = new HashMap();
        zmap.put("data",data);
        zmap.put("total",total);
       // System.out.println("1111111111111111111");
        //System.out.println(map);
        return zmap;
    }

    /**
     * 角色管理
     * @param map
     * @return
     */
    @RequestMapping("/update")
    @ResponseBody
    public Integer update(@RequestBody Map map){
        System.out.println(map+"sss");
        int row=service.update(map);
        return row;
    }

    /**
     * 新增角色
     * @param map
     * @return
     */
    @RequestMapping("/add")
    @ResponseBody
    public Integer add(@RequestBody Map map){
        System.out.println(map);
        int row=service.add(map);
        return row;
    }

    /**
     * 获取当前角色拥有的权限
     * @param rid
     * @return
     */
    @RequestMapping("/listid")
    @ResponseBody
    public Object tree(Integer rid){
       System.out.println(rid);
        List<F_Menu> list  = service.selectListid(rid);
        List<F_TreeVo> treeList = new ArrayList<F_TreeVo>();//记录树菜单数据
        int m = 0;//标记treeList的长度不会随每次添加数据而改变,防止报错
        for (F_Menu t: list) {
            Integer pid = t.getPid();//获取菜单的父id   1    0    系统管理   url
            F_TreeVo ovo = new F_TreeVo();//树数据vo类
            ovo.setId(t.getId());
            ovo.setLabel(t.getName());
            ovo.setUrl(t.getUrl());
            ovo.setPid(t.getPid());
            ovo.setChildren(new ArrayList<F_TreeVo>());
            m = treeList.size();
            if(pid==0){
                treeList.add(ovo);//一级菜单直接加入
            }else{
                for (int i=0;i<m;i++) {//二级菜单,按pid添加到一级菜单下
                    F_TreeVo vo = treeList.get(i);
                    if(vo!=null && pid==vo.getId()){
                       vo.getChildren().add(ovo);
                       break;
                    }
                }
            }
        }
      //System.out.println(treeList+"1111111111111111111111111111111");
        return treeList;

    }
    /**
     * 获取当前角色拥有的权限
     * @param rid
     * @return
     */
    @RequestMapping("/listid2")
    @ResponseBody
    public Object tree2(Integer rid){

        List<Map> list  = service.selectListid2(rid);
        Map zmap = new HashMap();
        zmap.put("data",list);
        System.out.println(zmap);
        return  zmap;
    }

    /**
     * 权限提交修改
     * @param map
     * @return
     */
    @RequestMapping("/update2")
    @ResponseBody
    public Integer update2(@RequestBody Map map){
      //System.out.println(map.get("ids"));
        //删除已有的权限
        service.del(map);
        int row=service.updaterRole(map);
        return 1;
    }

}
